﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OVS.Business;
using OVS.CoreDomain.Business;
using OVS.CoreDomain;

namespace OVS
{
    public partial class Vote : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoggedIn"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            if (!Page.IsPostBack)
            {
                IElection election = new Election();
                election.ElectionId = 1;

                IBusinessElection businessElection = new BusinessElection();
                election = businessElection.GetElection(election.ElectionId);

                if (DateTime.Now.ToShortDateString() != election.ElectionDate.ToShortDateString())
                {
                    lblErrMsg.Text = "This page is available only on the election date";
                    divVote.Visible = false;
                }
                else
                {


                    ILoggedin loggedIn = (ILoggedin)Session["LoggedIn"];

                    string voterId = loggedIn.VoterID;
                    IBusinessVoter businessVoter = new BusinessVoter();

                    IVoter voter = new Voter();
                    voter.VoterId = voterId;
                   

                    IVoter _voter = businessVoter.HaveVoted(voter, election);
                    if (_voter.HaveVoted)
                    {
                        divHaveVoted.Visible = true;
                        divVote.Visible = false;
                    }
                    else
                    {
                        divHaveVoted.Visible = false;
                        divVote.Visible = true;
                        PopulateConstituency(voterId);
                    }
                }
            }
        }

        protected void PopulateConstituency(string voterId)
        {
           
            IBusinessVoter businessVoter = new BusinessVoter();
            IVoter voter = businessVoter.GetVoter(voterId);

            ((DropDownList)SelectWard.FindControl("ddlDistrict")).SelectedValue = voter.DistrictId;

            SelectWard.FillPanchayat(voter.DistrictId);

            ((DropDownList)SelectWard.FindControl("ddlPanchayat")).SelectedValue = voter.PanchayatId;
            SelectWard.FillWards(voter.PanchayatId);

            ((DropDownList)SelectWard.FindControl("ddlWard")).SelectedValue = voter.WardId;

            ((DropDownList)SelectWard.FindControl("ddlDistrict")).Enabled = false;
            ((DropDownList)SelectWard.FindControl("ddlPanchayat")).Enabled = false;
            ((DropDownList)SelectWard.FindControl("ddlWard")).Enabled = false;

            IConstituency constituency = new Constituency();
            IDistrict district = new District();
            district.DistrictId = voter.DistrictId;

            IPanchayat panchayat = new Panchayat();
            panchayat.PanchayatId = voter.PanchayatId;

            IWard ward = new Ward();
            ward.WardId = voter.WardId;


            constituency.District = district;
            constituency.Panchayat = panchayat;
            constituency.Ward = ward;

            IElection election = new Election();
            election.ElectionId = 1;
            constituency.Election = election;

            PopulateCandidates(constituency);



        }


        protected void btnVote_click(object sender, EventArgs e)
        {
            IBusinessCandidate businessCandidate = new BusinessCandidate();
            ICandidate candidate = new Candidate();
            candidate.CandidateId = int.Parse(hidCandidateId.Value);
            IVoter voter = new Voter();

            ILoggedin loggedIn = (ILoggedin)Session["LoggedIn"];

            voter.VoterId = loggedIn.VoterID;

           // voter.VoterId = Session["VoterId"].ToString();

            IElection election = new Election();
            election.ElectionId = 1;

            IConstituency constituency = new Constituency();
            constituency.Election = election;

            candidate.Constituency = constituency;
            businessCandidate.UpdateVote(candidate,voter);
            Response.Redirect("~/ElectionResults.aspx");


        }
        protected void rdoVote_OnCheckedChanged(object sender, EventArgs e)
        {
            int i = 0;
            foreach (GridViewRow gvr in gvCandidateList.Rows)
            {
                ((RadioButton) gvCandidateList.Rows[i].FindControl("rdoVote")).Checked = false;
                i++;
            }

            RadioButton rdVote = (RadioButton)sender;
            rdVote.Checked = true;

            int candidateId =
                (int)gvCandidateList.DataKeys[((GridViewRow)((rdVote).BindingContainer)).RowIndex].Value;
            hidCandidateId.Value = candidateId.ToString();
        }

        protected void PopulateCandidates(IConstituency constituency)
        {
            IBusinessCandidate businessCandidate = new BusinessCandidate();
            IList<ICandidate> candidates = businessCandidate.GetCandidates(constituency);

            foreach (ICandidate candidate in candidates)
            {
                candidate.Voter.Photo = Page.ResolveClientUrl(@"~/Photos/Voters/" + candidate.Voter.VoterId + ".jpg");
                candidate.Symbol = Page.ResolveClientUrl(@"~/Photos/CandidateSymbols/" + candidate.Symbol + ".jpg");

            }
            gvCandidateList.DataSource = candidates;
            gvCandidateList.DataBind();

        }
    }
}
